package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDTO;
import com.zzyl.dto.ProjectPlanDTO;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVO;
import com.zzyl.vo.ProjectPlanVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    NursingPlanProjectMapper nursingPlanProjectMapper;

    @Override
    public List<NursingPlanVO> getAllPlan() {
        List<NursingPlanVO> list = nursingPlanMapper.getAllPlan();
        return list;
    }

    @Override
    @Transactional
    public void addPlan(NursingPlanDTO dto) {
        NursingPlan pojo = new NursingPlan();
        BeanUtils.copyProperties(dto,pojo);
        nursingPlanMapper.addPlan(pojo);

        List<ProjectPlanDTO> projectPlans = dto.getProjectPlans();
        projectPlans.stream().map(projectPlan -> {
            NursingPlanProject nursingPlanProject = new NursingPlanProject();
            projectPlan.setPlanId(pojo.getId().intValue());
            BeanUtils.copyProperties(projectPlan,nursingPlanProject);
            return nursingPlanProject;
        }).forEach(nursingPlanProject -> nursingPlanProjectMapper.addPlanProject(nursingPlanProject));

    }

    @Override
    public ResponseResult list(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVO> list = nursingPlanMapper.list(name,status);
        NursingPlanVO nursingPlanVO = new NursingPlanVO();
        PageResponse of = PageResponse.of(list, nursingPlanVO.getClass());
        return ResponseResult.success(of);
    }

    @Override
    @Transactional
    public NursingPlanVO getById(Integer id) {
        NursingPlanVO nursingPlanVO = nursingPlanMapper.getById(id);
        List<ProjectPlanVO> list = nursingPlanProjectMapper.getByPlanId(nursingPlanVO.getId());
        nursingPlanVO.setProjectPlans(list);
        return nursingPlanVO;
    }

    @Override
    @Transactional
    public void updatePlan(NursingPlanDTO dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlanMapper.updatePlan(nursingPlan);
        List<ProjectPlanDTO> projectPlans = dto.getProjectPlans();
        projectPlans.stream().map(projectPlan -> {
            NursingPlanProject nursingPlanProject = new NursingPlanProject();
            BeanUtils.copyProperties(projectPlan,nursingPlanProject);
            nursingPlanProject.setPlanId(dto.getId().intValue());
            return nursingPlanProject;
        }).forEach(nursingPlanProject -> {
            nursingPlanProjectMapper.delete(nursingPlanProject.getPlanId());
            nursingPlanProjectMapper.addPlanProject(nursingPlanProject);
        });
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        nursingPlanMapper.delete(id);
        nursingPlanProjectMapper.delete(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }


}
